package org.poem.tool;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author sangfor
 */
public class GatherBuilder {

    private static final Logger logger = LoggerFactory.getLogger( GatherBuilder.class );

    /**
     * 数据库类型
     */
    private String sourceType;

    /**
     * 地址
     */
    private String ip;

    /**
     * 端口号
     */
    private Integer port;

    /**
     * 用户名
     */
    private String userName;

    /**
     * 用户密码
     */
    private String password;


    /**
     * oracle 服务名字
     */
    private String serviceName;
    /**
     * 目标数据库
     */
    private String sourceSchema;

    /**
     * @param ip
     * @return
     */
    public GatherBuilder ip(String ip) {
        this.ip = ip;
        return this;
    }

    public GatherBuilder port(Integer port) {
        this.port = port;
        return this;
    }

    public GatherBuilder sourceSchema(String sourceSchema) {
        this.sourceSchema = sourceSchema;
        return this;
    }
    /**
     * 数据库类型
     * @param sourceType
     * @return
     */
    public GatherBuilder sourceType(String sourceType) {
        this.sourceType = sourceType;
        return this;
    }
    public GatherBuilder user(String user) {
        this.userName = user;
        return this;
    }

    public GatherBuilder password(String password) {
        this.password = password;
        return this;
    }



    public GatherBuilder serverName(String serviceName) {
        this.serviceName = serviceName;
        return this;
    }


    /**
     * @return
     * @throws ClassNotFoundException
     */
    public GatherConnection builder() {
        logger.info(" [ip]:" + ip + "  [password]:" + password + " [userName]:" + userName + " [port]:" + port);
        if (StringUtils.isBlank( ip )) {
            throw new IllegalArgumentException( "ip not null !!!!!!!" );
        }
        if (StringUtils.isBlank( password )) {
            throw new IllegalArgumentException( "password not null !!!!!!! " );
        }
        if (StringUtils.isBlank( userName )) {
            throw new IllegalArgumentException( "user not null !!!!!!! " );
        }
        if (port == null || port <= 0) {
            throw new IllegalArgumentException( "port not null !!!!!!! " );
        }

        String url = null;
        try {
            if ("mysql".equalsIgnoreCase(this.sourceType)){
                Class.forName( "com.mysql.cj.jdbc.Driver" );
                url = "jdbc:mysql://" + ip + ":" + port + "?useSSL=false&serverTimezone=UTC";
            }else if ("sqlserver".equalsIgnoreCase(this.sourceType)){
                Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
                url = "jdbc:sqlserver://" + ip + ":" + port + ";DatabaseName=" + sourceSchema;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            logger.error( e.getLocalizedMessage(), e );
        }
        if (url == null) {
            throw new NullPointerException( "url is null!!!!!!" );
        }
        return new GatherConnection( url, password, userName );
    }
}
